<?php

declare(strict_types=1);

namespace App\Chat\Controller;

use App\Chat\Annotation\ChatAuth;
use App\Chat\Request\TalkUrlRequest;
use App\Chat\Service\TalkUrlService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\PostMapping;

#[Controller(prefix: 'ch/chat/url'),ChatAuth]
class UrlController extends ChatBaseController
{
    #[Inject]
    protected TalkUrlService $service;

    /**
     * 列表
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('getList')]
    public function getList(): \Psr\Http\Message\ResponseInterface
    {
        $data = $this->request->all();
        $data['select'] = 'id,name,url,desc,icon';
        return $this->success($this->service->getPageList($data));
    }

    /**
     * 保存链接.
     * @param TalkUrlRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('save')]
    public function save(TalkUrlRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->success(['id'=>$this->service->save($request->all())]);
    }

    /**
     * 详情
     * @param TalkUrlRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('info')]
    public function info(TalkUrlRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->success($this->service->read($request->input('id'),['id','name','url','desc','icon']));
    }

    /**
     * 修改链接
     * @param TalkUrlRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('update')]
    public function update(TalkUrlRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->service->update($request->input('id'),$request->all()) ? $this->success() : $this->error();
    }

    /**
     * 删除链接
     * @param TalkUrlRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('delete')]
    public function delete(TalkUrlRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->service->delete([$request->input('id')]) ? $this->success() : $this->error();
    }
}